package com.sherlockHolmes.init.controller;

import com.sherlockHolmes.init.base.utils.IPUtils;
import com.sherlockHolmes.init.recommend.PeopleNearby;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/ip")
@Slf4j
public class AddressController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private PeopleNearby peopleNearby;
    @RequestMapping("/get")
    public void getAddress(HttpServletRequest request) {
        try {
            log.info(IPUtils.getIpAddress(request));
            double[] longitudes = {116.40001, 116.40023, 116.40057, 116.40102, 116.40158, 116.40226, 116.40306, 116.40398, 116.40503, 116.40622};
            double[] latitudes = {39.90001, 39.90015, 39.90036, 39.90064, 39.90102, 39.90150, 39.90209, 39.90281, 39.90367, 39.90467};
            String[] names={"李毅","波奇","阿米利卡","等天下雨","小明","小红","小绿","小蓝","小黄","小紫"};
            for (int i = 0; i < longitudes.length; i++) {
                for (int j = 0; j < latitudes.length; j++) {
                    redisTemplate.opsForGeo().add("geo", new Point(longitudes[i], latitudes[j]), names[i]+j);
                }
            }
            GeoResults results = peopleNearby.radiusByMember("波奇1", 150, Metrics.METERS);
            List<GeoResult> content = results.getContent();
            content.forEach(result -> {
                RedisGeoCommands.GeoLocation d= (RedisGeoCommands.GeoLocation) result.getContent();
                d.getName();
                d.getPoint();
                result.getDistance();
                System.out.println( d.getName()+"-------"+d.getPoint()+"-----------"+result.getDistance());
            });
// 这些经纬度基于北京市中心（约116.40000, 39.90000），但它们之间的实际距离并未精确控制在3km左右。
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
